package com.right.health.app.ui.medical.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.magic.basic.adapter.BaseAbsAdapter;
import com.right.health.app.R;
import com.right.health.app.bean.AllergySourceResponse;


public class AllergyListAdapter extends BaseAbsAdapter<AllergySourceResponse.AllergySourceInfo> {

    public AllergyListAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.allergy_list_item, null);
            viewHolder = new ViewHolder();

            viewHolder.nameTv = (TextView) convertView.findViewById(R.id.tv_tips1);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.cb_selected1);
            viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int pos = (int)buttonView.getTag();
                    AllergySourceResponse.AllergySourceInfo info = mDataSource.get(pos);
                    info.anaphylactogenstatus = isChecked ? "1" : "0";
                    mDataSource.set(pos, info);
                }
            });
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AllergySourceResponse.AllergySourceInfo info = mDataSource.get(position);

        viewHolder.checkBox.setTag(position);
        viewHolder.nameTv.setText(info.anaphylactogenname);
        viewHolder.checkBox.setChecked("1".equals(info.anaphylactogenstatus));
        return convertView;
    }

    private class ViewHolder {
        private TextView nameTv;
        private CheckBox checkBox;
    }
}
